package com.fjh.controller;

import com.fjh.pojo.Activity;
import com.fjh.pojo.Student;
import com.fjh.pojo.Teacher;
import com.fjh.pojo.TeachingClass;
import com.fjh.service.ActivityService;
import com.fjh.service.TeachingClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teaching-classes")
public class TeachingClassController {

    @Autowired
    private TeachingClassService teachingClassService;

    @Autowired
    private ActivityService activityService;

    @PostMapping
    public ResponseEntity<TeachingClass> createTeachingClass(@RequestBody TeachingClass teachingClass) {
        try {
            Long activityId = (teachingClass.getActivity() != null) ? teachingClass.getActivity().getId() : null;

            Activity activity = activityService.findActivityById(activityId);

            if (activity != null && activity.getStatus() == 1) {
                teachingClass.setActivity(activity);
                TeachingClass createdTeachingClass = teachingClassService.createTeachingClass(teachingClass);
                return ResponseEntity.status(HttpStatus.CREATED).body(createdTeachingClass);
            } else {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @PostMapping("/{classId}/add-students")
    public ResponseEntity<TeachingClass> addStudentsToClass(@PathVariable Long classId, @RequestBody List<Student> students) {
        TeachingClass updatedTeachingClass = teachingClassService.addStudentsToClass(classId, students);
        return ResponseEntity.ok(updatedTeachingClass);
    }

    @PostMapping("/{classId}/assign-teacher")
    public ResponseEntity<TeachingClass> assignTeacherToClass(@PathVariable Long classId, @RequestBody List<Teacher> teachers) {
        TeachingClass updatedTeachingClass = teachingClassService.assignTeacherToClass(classId, teachers);
        return ResponseEntity.ok(updatedTeachingClass);
    }

}
